import Vue from 'vue'
import VueRouter from 'vue-router'
// import Layout from '@/Layout'

Vue.use(VueRouter)
// 解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}
// 固定路由
export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login')
  }, {
    path: '/',
    redirect: '/dashboard',
    component: () => import('@/Layout'),
    children: [
      {
        path: '/dashboard',
        component: () => import('@/views/Dashboard/index'),
        name: 'Dashboard',
        meta: { name: '仪表页' }
      }
    ]
  }
]
// 动态路由
export const asyncRoutes = [
  {
    path: '/roles',
    component: () => import('@/Layout'),
    redirect: '/roles/pages',
    meta: { name: '权限页面' },
    children: [
      {
        path: '/pages',
        name: '权限',
        component: () => import('@/views/Roles/page'),
        meta: { name: '权限', roles: 'admin' }
      }, {
        path: '/test',
        name: '测试',
        component: () => import('@/views/Roles/test'),
        meta: { roles: 'admin' }
      }
    ]
  },
  {
    path: '/theme',
    component: () => import('@/Layout'),
    redirect: '/theme/pages',
    children: [
      {
        path: '/pagedemo',
        name: '主题',
        component: () => import('@/views/Theme/page.vue'),
        meta: { roles: 'admin' }
      }
    ]
  }
]
const createRouter = () => new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
const router = createRouter()
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
